﻿using Furion.DependencyInjection;
using Furion.DynamicApiController;
using HiSport.Core.Options;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using NewLife.Caching;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HiSport.Core
{
    /// <summary>
    /// 系统缓存服务
    /// </summary>
    [ApiDescriptionSettings(Order = 400)]
    public class SysCacheService : IDynamicApiController, ISingleton
    {
        private readonly ICache _cache;
        private readonly CacheOptions _cacheOptions;

        public SysCacheService(ICache cache, IOptions<CacheOptions> cacheOptions)
        {
            _cache = cache;
            _cacheOptions = cacheOptions.Value;
        }

        /// <summary>
        /// 获取缓存键名集合
        /// </summary>
        /// <returns></returns>
        [DisplayName("获取缓存键名集合")]
        public List<string> GetKeyList()
        {
            // 键名去掉全局缓存前缀
            return _cache.Keys.Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList();
        }

        /// <summary>
        /// 增加缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        [NonAction]
        public bool Set(string key, object value)
        {
            return _cache.Set($"{_cacheOptions.Prefix}{key}", value);
        }

        /// <summary>
        /// 增加缓存并设置过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expire"></param>
        /// <returns></returns>
        [NonAction]
        public bool Set(string key, object value, TimeSpan expire)
        {
            return _cache.Set($"{_cacheOptions.Prefix}{key}", value, expire);
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        [NonAction]
        public T Get<T>(string key)
        {
            return _cache.Get<T>($"{_cacheOptions.Prefix}{key}");
        }

        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        [ApiDescriptionSettings(Name = "Delete"), HttpPost]
        [DisplayName("删除缓存")]
        public int Remove(string key)
        {
            return _cache.Remove($"{_cacheOptions.Prefix}{key}");
        }

        /// <summary>
        /// 检查缓存是否存在
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        [NonAction]
        public bool ExistKey(string key)
        {
            return _cache.ContainsKey($"{_cacheOptions.Prefix}{key}");
        }

        /// <summary>
        /// 根据键名前缀删除缓存
        /// </summary>
        /// <param name="prefixKey">键名前缀</param>
        /// <returns></returns>
        [ApiDescriptionSettings(Name = "DeleteByPreKey"), HttpPost]
        [DisplayName("根据键名前缀删除缓存")]
        public int RemoveByPrefixKey(string prefixKey)
        {
            var delKeys = _cache.Keys.Where(u => u.StartsWith(prefixKey)).ToArray();
            if (!delKeys.Any()) return 0;
            return _cache.Remove(delKeys);
        }

        /// <summary>
        /// 根据键名前缀获取键名集合
        /// </summary>
        /// <param name="prefixKey">键名前缀</param>
        /// <returns></returns>
        [DisplayName("根据键名前缀获取键名集合")]
        public List<string> GetKeysByPrefixKey(string prefixKey)
        {
            return _cache.Keys.Where(u => u.StartsWith(prefixKey)).ToList();
        }

        /// <summary>
        /// 获取缓存值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        [DisplayName("获取缓存值")]
        public object GetValue(string key)
        {
            return _cache.Get<object>($"{_cacheOptions.Prefix}{key}");
        }

        /// <summary>
        /// 获取或添加缓存，在数据不存在时执行委托请求数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="callback"></param>
        /// <param name="expire">过期时间，单位秒</param>
        /// <returns></returns>
        [NonAction]
        public T GetOrAdd<T>(string key, Func<string, T> callback, int expire = -1)
        {
            return _cache.GetOrAdd($"{_cacheOptions.Prefix}{key}", callback, expire);
        }
    }
}
